
layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data: []}, // 配置项
		f: function (c) { // 数据初始化
			var s = new Date().getTime(), t = this;
			t.c.id = c.id ? c.id : 'simple_table_WF_Store';
			t.c.tableId = c.tableelem ? c.tableelem : 'table-' + s;
			t.c.data = c.data; // 数据

			//将表单数据带入js
			t.c.formObj = c.formObj;
			/**自定义的字段，是否流转中**/
			t.c.onProcess = c.onProcess;

            //复选框选中，存放行号
            t.c.checkedIndex = [];

		},
		load: function () { // 加载控件
			var h = ``
				+ `<div class="layui-form layui-form-item" lay-filter="` + this.id + `" style="background-color: #ffffff;padding-top: 10px;padding-bottom: 10px;">`
				+ `<label style="font-weight: 700;font-size: 18px; padding: 15px;">店铺信息</label>`
				+ `</div>`
			if (this.c.onProcess == 0) {
				h += `<div class="layui-table-tool" style="padding-top: 0; min-height: 0; background-color: #ffffff;width: auto;">`
					+ `<div class="layui-table-tool-temp">`
					+ `<div class="layui-btn-container">`
					+ `<a class="layui-btn layui-btn-normal layui-btn-sm " lay-event="event-to-select"><i class="iconfont icon-add-btn"></i>` + '新增' + `</a>`
					+ `<a class="layui-btn layui-btn-danger layui-btn-sm " lay-event="event-to-del" ><i class="layui-icon layui-icon-delete">删除</a>`
                    + `</div>`
					+ `</div>`
					+ `</div>`
			}
			h += `<div class="layui-form layui-form-item" pane>`
				+ `<div id="` + this.c.tableId + `">`
				+ this.loadTable()
				+ `</div>`
				+ `</div>`
				+ `</div>`;
			$('#' + this.c.id).html(h);
			this.listenBtn();
            this.listenBtnByTd();
		},
		loadTable: function () { // 加载表格
			return ``
				+ `<div class="layui-table-box">`
				+ `<div class="layui-table-header" style="height: 300px; max-height: 300px; overflow: auto; background-color: #ffffff;">`
				+ `<table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="grid" lay-even="" style="margin: 0;">`
				+ `<thead>`
				+ this.loadTableOfThead()
				+ `</thead>`
				+ `<tbody>` + this.loadTableOfTbody() + `</tbody>`
				+ `</table>`
				+ `</div>`
				+ `</div>`;
		},
		loadTableOfThead: function () { // 加载表头
			var h = `<thead>`;
			h += `<tr>`;
			h += `<th style="display: none;">id</th>`;
            if (this.c.onProcess == 0) {
                h += `<th style="width:1.9%; text-align: center; color: #000000;padding: 0 !important;">
							<input type="checkbox" id="checkAll" lay-skin="primary" lay-filter="checkAll">
						</th>`;
            }
			h += `<th style="width:5%; text-align: center; color: #000000;">网店账号</th>`;
			h += `<th style="width:5%; text-align: center; color: #000000;">网店名称</th>`;
			h += `<th style="width:5%; text-align: center; color: #000000;">公司编码</th>`;
			h += `<th style="width:5%; text-align: center; color: #000000;">公司简称</th>`;
			h += `</tr>`;
			h + `</thead>`;
			return h;
		},

        loadTableOfTbody: function () { // 加载表tbody
            var t = this;
			var h = `<tbody id="tbody">`;
			for(var i=0;i<t.c.data.length;i++){
				h += `<tr id="table_th_`+i+`">`;
				h += t.loadTableTdOfTbody(t.c.data[i],i);
				h+= `</tr>`;
			}
			h += `</tbody>`;
			return h;
        },
        loadTableTdOfTbody: function (data,i) { // 加载表tbody
            var h = `<td style="display: none;">` + data.id + `</td>`;
            h += `<td style="display: none;">` + data.amazonShopId + `</td>`;
            if (this.c.onProcess == 0) {
                h += `<td style="text-align:center;padding:0;">
					<input type="checkbox" class="chBox"  lay-skin="primary" data-index="` + i + `"  lay-filter="checkBoxOne">
					</td>`;
            }
			h += `<td style="text-align:center;padding: 0;">` + data.shopUser + `</td>`;

			h += `<td style="text-align:center;padding: 0;">` + data.shopName + `</td>`;
            h += `<td style="text-align:center;padding:0;">` + data.vgsbm + `</td>`;
            h += `<td style="text-align:center;padding:0;">` + data.vgsjc + `</td>`;
            h += `</tr>`;
			return h;
		},
		listenBtn: function () { // 添加监听
			var t = this;

			// 监听“新增”按钮
			$('a[lay-event="event-to-select"]').on('click', function (e) {
				var selectedData = [];
				//回填数据的代码
				for (var i = 0; i < t.c.data.length; i++) {
					var row = {};
					row.id = t.c.data[i].amazonShopId;
					row.shopUser = t.c.data[i].shopUser;
					row.shopName = t.c.data[i].shopName;
					row.vgsbm = t.c.data[i].vgsbm;
					row.vgsjc = t.c.data[i].vgsjc;
					selectedData.push(row);
				}
				layui.YsModal.init({
					modalKey: 'Tp_AmazonShopWs',
					type: 'checkbox',
					uniqueKey: 'id',
					parentObj: t,
					selectedData: selectedData,
					sameCode: 'vgsbm' // 选取结果唯一值限制字段
				})

			});

            //全选
            layui.form.on('checkbox(checkAll)', function(data){
                var elem = data.elem;
                $(".chBox").prop("checked", elem.checked);
                var checkedIndex = [];
                $(".chBox").each(function(k, v){
                    if ($(v).is(':checked')) {
                        checkedIndex.push($(v).attr('data-index'));
                    }
                });
                t.c.checkedIndex = checkedIndex;
                layui.form.render();
            });
            // 列表里的小选择框点击
            layui.form.on('checkbox(checkBoxOne)', function(data){
                var elem = data.elem;
                if ($(".chBox").not('input:checked').length) {
                    $('#checkAll').prop("checked", false);
                } else {
                    $('#checkAll').prop("checked", true);
                }
                var checkedIndex = [];
                $(".chBox").each(function(k, v){
                    if ($(v).is(':checked')) {
                        checkedIndex.push($(v).attr('data-index'));
                    }
                });
                t.c.checkedIndex = checkedIndex;
                layui.form.render();
            });


            // 监听“删除”按钮
            $('a[lay-event="event-to-del"]').on('click',function (e) {
                var checkedIndexList = t.c.checkedIndex;
                if (checkedIndexList.length ==0 || checkedIndexList==null){
                    return layer.msg("请至少选择一条要删除的数据!");
                }

				var newData = [];
				for (var i = 0; i < t.c.data.length; i++) {
					var isDel = false;
					for (var j = 0; j < checkedIndexList.length; j++) {
						if(i == checkedIndexList[j]){
							isDel = true;
						}
					}
					if(!isDel){
						newData.push(t.c.data[i]);
					}
				}
				t.c.data = newData;
				$('#' + t.c.id).empty();
				return t.reload(t.c);


            });
		},

        listenBtnByTd:function () { // 添加监听
            var t = this;
            var domTable = $('#' + t.c.tableId);

            // 表单控件重新加载
            form.render();
        },

		isFrom1_0: function() {
			if(window.top.isMobile!=undefined || window.top.isMobile!=null) {
				return false;
			} else {
				return true;
			}
		},
		openUrl: function(title, url, toType) { // toType，打开页面方式，1标签页2新页面
			var isOn1_0 = this.isFrom1_0();
			if (isOn1_0 == true) {
				window.open(url);
			} else {
				if (toType != null && toType != undefined) {
					if (toType == 1) {
						window.top.add_tab(title,url);
					} else {
						window.open(url);
					}
				}
			}
		},


		removeRow: function (index) { // 重新加载表格
			var t = this;
			t.c.data.splice(index, 1);
			return t.reload(t.c);
		},
		reloadTable: function () { // 重新加载表格
			$('#' + this.c.tableId).html(this.loadTable());
			this.listenBtn();
		},
		getData: function () {
			return this.c.data;
		},
		init: function (c) {
			this.f(c);
			this.load();
			return object;
		},
		reload: function (c) {
			c.data = this.c.data;
			$('#' + this.c.id).empty();
			return this.init(c);
		},
		addRow: function (row) {
			var c = this.c;
            var t = this;
			c.data.push(row);
            var h =  `<tr id="table_th_`+(c.data.length-1)+`">`;
            h += t.loadTableTdOfTbody(row,c.data.length-1);
            h+= `</tr>`;
            $('#tbody').append(h);
            t.listenBtnByTd();
		},
		addRows: function (rows) {
			var c = this.c;
			for (var i = 0; i < rows.length; i++) {
				c.data.push(rows[i]);
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		addRowsDx:function (rows) {
			var c = this.c;
			c.data = [];
			for (var i = 0; i < rows.length; i++) {
				c.data.push(rows[i]);
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		editRow: function (index, row) {
			var c = this.c;
			c.data[index] = row;
			$('#' + c.id).empty();
			return this.reload(c);
		},
		editRowOfAttr: function (index, row) {
			var c = this.c;
			for (var key in row) {
				c.data[index][key] = row[key]
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		reloadRows: function (rows) {
			var c = this.c;
			c.data = rows;
			return this.reload(c);
		},
		//重写toFixed方法
		toFixed: function (number, precision) {
			if (Number.isInteger(Number(number))) {
				return number;
			}
			number = Math.round(+number + 'e' + precision) / Math.pow(10, precision) + '';
			let s = number.split('.');
			if ((s[1] || '').length < precision) {
				s[1] = s[1] || '';
				s[1] += new Array(precision - s[1].length).fill('0').join('');
			}
			return s.join('.');
		},

		//多选框
		matchValuesAfterModalSelectCheckbox: function (obj1, obj2, obj3) {
			var t = this;
			var rowCurrents = [];
			const vgsBmSet = new Set();
			for (var i = 0; i < obj1.length; i++) {
				var obj = obj1[i];
				var rowCurrent = {};
				rowCurrent.id = null;
				rowCurrent.shopUser = obj.shopUser;
				rowCurrent.shopName = obj.shopName;
				rowCurrent.vgsbm = obj.vgsbm;
				rowCurrent.vgsjc = obj.vgsjc;
				rowCurrent.amazonShopId = obj.id
				rowCurrents.push(rowCurrent);
				vgsBmSet.add(rowCurrent.vgsBm);
			}
			t.addRowsDx(rowCurrents);
		},
	}
	exports('simple_table_WF_Store', object);
});